package com.clas.liu.myapplication.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.model.entity.info.Info;
import com.clas.liu.myapplication.view.activity.impl.ImageViewActivity;
import com.clas.liu.myapplication.view.activity.impl.InfoActivity;
import com.clas.liu.myapplication.view.activity.impl.SearchActivity;
import com.clas.liu.myapplication.view.activity.impl.UserInfoActivity;
import com.clas.liu.myapplication.view.fragment.impl.SquareFragment;
import com.clas.liu.myapplication.view.util.DateUtil;
import com.clas.liu.myapplication.view.util.NetworkHelper;
import com.clas.liu.myapplication.view.util.SomeResources;
import com.clas.liu.myapplication.view.widget.CircleImageView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liu on 2019/5/4.
 */

public class SquareItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {

    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_HEADER = -1;
    private Context context;
    private List<Info> data;
    private SquareFragment fragment;
    private boolean isDelete = false;

    public SquareItemAdapter(Context context, List<Info> data, SquareFragment fragment) {
        this.context = context;
        this.data = data;
        this.fragment = fragment;


    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE_HEADER:
                holder = new HeaderHolder(LayoutInflater.from(context).
                        inflate(R.layout.item_square_search_view,parent,false));
                break;
            case TYPE_ITEM:
                holder = new MyHolder(LayoutInflater.from(context).
                        inflate(R.layout.item_square_info,parent,false));
                break;
            case TYPE_FOOTER:
                holder = new FooterHolder(LayoutInflater.from(context).
                        inflate(R.layout.item_footer_view,parent,false));
                break;
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else if (position == 0){
            return TYPE_HEADER;
        }else
            return TYPE_ITEM;

    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEADER:
                HeaderHolder headerHolder = (HeaderHolder)holder;
                headerHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(new Intent(context, SearchActivity
                                .class));
                    }
                });
                break;
            case TYPE_FOOTER:
                FooterHolder holder1 = (FooterHolder)holder;
                holder1.foot_view_item_tv.setText("正在加载...");
                if(isDelete){
                    holder1.progressBar.setVisibility(View.GONE);
                    holder1.foot_view_item_tv.setText("没有更多了哦~");
                    holder1.foot_view_item_tv.setAlpha(0.5f);
                    holder1.foot_view_item_tv.setTextColor(context.getResources().getColor(R.color.black));
                }
                if(!NetworkHelper.isNetworkAvailable(context) && data.size()==0){
                    holder1.progressBar.setVisibility(View.GONE);
                    holder1.foot_view_item_tv.setVisibility(View.VISIBLE);
                    holder1.loadLayout.setVisibility(View.GONE);
                }else{
                    holder1.foot_view_item_tv.setVisibility(View.VISIBLE);
                    holder1.loadLayout.setVisibility(View.VISIBLE);
                }
                break;
            case TYPE_ITEM:
                MyHolder holder2 = (MyHolder)holder;
                final Info info = data.get(position-1);
                holder2.name.setText(info.getName());
                Picasso.with(context).load(info.getHead()).placeholder(R.mipmap.default_head).
                        error(R.mipmap.loading).into(holder2.head);
                holder2.category.setText(SomeResources.getCategorys().get(info.getCategory()));
                holder2.category.setBackgroundResource(SomeResources.getCategoryDrawables().get(info.getCategory()));
                if (info.getSex().equals("男"))
                    holder2.sex.setImageResource(R.mipmap.man);
                else
                    holder2.sex.setImageResource(R.mipmap.woman);
                Date date = new Date(Long.parseLong(info.getRelease_time()));
                holder2.time.setText(DateUtil.getFormatTime(date));
                holder2.text.setText(info.getText());
                if (info.getPic()!=null && !info.getPic().equals("")&&!info.getPic().equals("null"))
                {
                    Log.i("test", "onBindViewHolder: "+info.getName()+info.getPic());
                    holder2.pic.setVisibility(View.VISIBLE);
                    Picasso.with(context).load(info.getPic()).placeholder(R.mipmap.loading).
                            error(R.mipmap.loading).into(holder2.pic);
                }else holder2.pic.setVisibility(View.GONE);
                if (info.getKey_word() !=null && !info.getKey_word().equals("") && !info.getKey_word().equals("null")) {
                    holder2.key.setVisibility(View.VISIBLE);
                    holder2.key.setText(new StringBuilder().append("关键词： ").append(info.getKey_word()).toString());
                }
                else holder2.key.setVisibility(View.GONE);
                holder2.pic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, ImageViewActivity.class);
                        intent.putExtra("url",info.getPic());
                        context.startActivity(intent);
                    }
                });
                holder2.head.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra("user_id",info.getUser_id());
                        context.startActivity(intent);
                    }
                });
                holder2.item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, InfoActivity.class);
                        intent.putExtra("info",info);
                        context.startActivity(intent);
                    }
                });
                break;

        }
    }

    @Override
    public int getItemCount() {
        return data.size()+2;
    }

    public void deleteLoadingMore(boolean isDelete){
        this.isDelete = isDelete;
        SquareItemAdapter.this.notifyItemChanged(getItemCount()-1);
    }

    public void update(List<Info> root){
        data = root;
        this.notifyDataSetChanged();
    }

    public class MyHolder extends RecyclerView.ViewHolder{

        CircleImageView head;
        TextView name;
        ImageView sex;
        TextView category;
        TextView time;
        TextView text;
        ImageView pic;
        TextView key;
        LinearLayout item;
        public MyHolder(View itemView) {
            super(itemView);
            head = itemView.findViewById(R.id.head);
            name = itemView.findViewById(R.id.name);
            sex = itemView.findViewById(R.id.sex);
            category = itemView.findViewById(R.id.category);
            time = itemView.findViewById(R.id.time);
            text = itemView.findViewById(R.id.text);
            pic = itemView.findViewById(R.id.pic);
            key = itemView.findViewById(R.id.key);
            item = itemView.findViewById(R.id.item);
        }
    }
    public class HeaderHolder extends RecyclerView.ViewHolder{

       LinearLayout linearLayout;
        public HeaderHolder(View itemView) {
            super(itemView);
           linearLayout = itemView.findViewById(R.id.square_search_layout);
        }
    }
    public class FooterHolder extends RecyclerView.ViewHolder{
        private TextView foot_view_item_tv;
        private LinearLayout loadLayout;
        private ProgressBar progressBar;

        public FooterHolder(View view) {
            super(view);
            foot_view_item_tv = (TextView) view.findViewById(R.id.tv);
            loadLayout = (LinearLayout)view.findViewById(R.id.loadmore);
            progressBar = (ProgressBar)view.findViewById(R.id.progress);
        }
    }
}
